package cn.edu360.day07.utile;

import ch.hsr.geohash.GeoHash;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod;

import java.io.IOException;

/**
 * Created by root on 2018/7/9.
 */
public class Utiles {
    /**
     * 经纬度转化为hash值
     * @param lng
     * @param lat
     * @return
     */
    public static String getGeoHashCode(double lng,double lat){
        String base32 = GeoHash.withCharacterPrecision(lat, lng, 8).toBase32();
        return base32;
    }


    /**
     * 使用httpClient从网络上获取json数据
     * @param lng
     * @param lat
     * @return
     */
    public static String getJsonByHttpClient(double lng,double lat){
        String uri = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + lng+"&type=010";
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(uri);
        try {
            client.executeMethod(method);
            String json = method.getResponseBodyAsString();
            return json;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
